package pub.wkq.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pub.wkq.bo.AdminBO;
import pub.wkq.bo.CreateAdminBO;
import pub.wkq.bo.ResetPwdBO;
import pub.wkq.common.base.BaseInfoProperties;
import pub.wkq.common.exception.CustomException;
import pub.wkq.common.result.ResponseStatusEnum;
import pub.wkq.common.utils.MD5Utils;
import pub.wkq.common.utils.PagedGridResult;
import pub.wkq.pojo.Admin;
import pub.wkq.user.mapper.AdminMapper;
import pub.wkq.user.service.AdminService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * <p>
 * 慕聘网运营管理系统的admin账户表，仅登录，不提供注册 服务实现类
 * </p>
 *
 * @author 王开琦
 * @since 2023-06-05
 */
@Service
public class AdminServiceImpl extends BaseInfoProperties implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Transactional
    @Override
    public void createAdmin(CreateAdminBO createAdminBO) {
        // admin 账号判断是否存在，如果存在则不允许账号分配
        Admin admin = getSelfAdmin(createAdminBO.getUsername());
        if (admin != null) {
            CustomException.thr(ResponseStatusEnum.ADMIN_USERNAME_EXIST_ERROR);
        }
        // 不存在则创建 admin 账号
        Admin newAdmin = new Admin();
        BeanUtils.copyProperties(createAdminBO, newAdmin);
        //随机随机数字或者因为字母组合的盐
        String salt = MD5Utils.getRandomSalt(8);
        String pwd = MD5Utils.encrypt(createAdminBO.getPassword(), salt);
        newAdmin.setPassword(pwd);
        newAdmin.setSlat(salt);
        newAdmin.setCreateTime(LocalDateTime.now());
        newAdmin.setUpdatedTime(LocalDateTime.now());
        adminMapper.insert(newAdmin);
    }

    @Override
    public PagedGridResult list(String accountName, Integer page, Integer limit) {
        startPage(page, limit);
        List<Admin> adminList = adminMapper.list(accountName);
        return setterPagedGrid(adminList, page);
    }

    @Override
    public void deleteAdmin(String username) {
        int deleteCount = adminMapper.deleteByUsername(username);
        if (deleteCount != 1) {
            CustomException.thr(ResponseStatusEnum.ADMIN_DELETE_ERROR);
        }
    }

    @Override
    public void resetPwd(ResetPwdBO resetPwdBO) {
        //1.判断用户是否存在
        Admin admin = adminMapper.selectById(resetPwdBO.getAdminId());
        if (admin == null) {
            CustomException.thr(ResponseStatusEnum.ADMIN_NOT_EXIST);
        }
        //2.设置新密码
        String salt = MD5Utils.getRandomSalt(8);
        String pwd = MD5Utils.encrypt(resetPwdBO.getPassword(), salt);
        admin.setPassword(pwd);
        admin.setSlat(salt);
        admin.setUpdatedTime(LocalDateTime.now());
        adminMapper.updateById(admin);
    }

    private Admin getSelfAdmin(String adminName) {
        return new LambdaQueryChainWrapper<>(adminMapper).eq(Admin::getUsername, adminName).one();
    }
}
